﻿// CMyDlgComBox.cpp: 实现文件
//

#include "pch.h"
#include "MY_PE_TOOL.h"
#include "CMyDlgComBox.h"
#include "afxdialogex.h"


// CMyDlgComBox 对话框

IMPLEMENT_DYNAMIC(CMyDlgComBox, CDialogEx)

CMyDlgComBox::CMyDlgComBox(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG3, pParent)
{

}

CMyDlgComBox::~CMyDlgComBox()
{
}

void CMyDlgComBox::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_COMBO1, m_cmbLocation);
}

//文件头中的Machine标志，将其相关数值插入链表
void CMyDlgComBox::PushListToFileHeaderMachine()
{
	FileHeader_Machine FHM;

	FHM.wValue = 0;
	FHM.strExplain = "UNKNOWN";
	m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0001;
    FHM.strExplain = "TARGET_HOST";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x014c;
    FHM.strExplain = "I386";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0162;
    FHM.strExplain = "R3000";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0166;
    FHM.strExplain = "R4000";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0168;
    FHM.strExplain = "R10000";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0169;
    FHM.strExplain = "WCEMIPSV2";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0184;
    FHM.strExplain = "ALPHA";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01A2;
    FHM.strExplain = "SH3";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01A3;
    FHM.strExplain = "SH3DSP";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01A4;
    FHM.strExplain = "SH3E";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01A6;
    FHM.strExplain = "SH4";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01A8;
    FHM.strExplain = "SH5";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01C0;
    FHM.strExplain = "ARM";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01C2;
    FHM.strExplain = "THUMB";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01C4;
    FHM.strExplain = "ARMNT";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01D3;
    FHM.strExplain = "AM33";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01F0;
    FHM.strExplain = "POWERPC";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x01F1;
    FHM.strExplain = "POWERPCFP";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0200;
    FHM.strExplain = "IA64";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0266;
    FHM.strExplain = "MIPS16";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0284;
    FHM.strExplain = "ALPHA64";
    m_FileHeadMachineData.push_back(FHM);
    
    FHM.wValue = 0x0366;
    FHM.strExplain = "MIPSFPU";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0466;
    FHM.strExplain = "MIPSFPU16";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0520;
    FHM.strExplain = "TRICORE";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0CEF;
    FHM.strExplain = "CEF";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x0EBC;
    FHM.strExplain = "EBC";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x8664;
    FHM.strExplain = "AMD64";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0x9041;
    FHM.strExplain = "M32R";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0xAA64;
    FHM.strExplain = "ARM64";
    m_FileHeadMachineData.push_back(FHM);

    FHM.wValue = 0xC0EE;
    FHM.strExplain = "CEE";
    m_FileHeadMachineData.push_back(FHM);
}


//将链表中的数据插入ComBox
void CMyDlgComBox::ShowDataToComBox()
{
    int nCount = 0;

    for (auto &Item : m_FileHeadMachineData)
    {
        m_cmbLocation.AddString(Item.strExplain);
        if (Item.wValue == *m_pwValue)
        {
            m_cmbLocation.SetCurSel(nCount);
        }

        nCount++;
    }
}


void CMyDlgComBox::SetValue(WORD* pwVal)
{
	m_pwValue = pwVal;
}

void CMyDlgComBox::SetFlag(DWORD dwFlag)
{
	m_dwFlag = dwFlag;
}


//设置文件路径
void CMyDlgComBox::SetFilePath(CString FilePath)
{
	m_FilePath = FilePath;
}


//设置文件偏移
void CMyDlgComBox::SetFileOffset(DWORD dwFileOffset)
{
    m_dwFileOffset = dwFileOffset;
}


BOOL CMyDlgComBox::MyWriteFile(void* pValue)
{
    //打开文件
    HANDLE hFile = CreateFile(m_FilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    {
        MessageBox(_T("打开文件失败"), _T("提示"), MB_OK);
        return FALSE;
    }

    //设置文件指针偏移
    DWORD D = SetFilePointer(hFile, m_dwFileOffset, NULL, FILE_BEGIN);

    //写文件
    DWORD dwWriteBytes = 0;
    BOOL bRet = WriteFile(hFile, pValue, 2, &dwWriteBytes, NULL);
    if (bRet == FALSE)
    {
        MessageBox(_T("写入文件失败"), _T("提示"), MB_OK);
        CloseHandle(hFile);
        return FALSE;
    }

    //关闭句柄
    CloseHandle(hFile);
    return TRUE;
}


BEGIN_MESSAGE_MAP(CMyDlgComBox, CDialogEx)
	ON_BN_CLICKED(BTN_OK, &CMyDlgComBox::OnBnClickedOk)
	ON_BN_CLICKED(BTN_CANCLE, &CMyDlgComBox::OnBnClickedCancle)
END_MESSAGE_MAP()


// CMyDlgComBox 消息处理程序


//OK按钮
void CMyDlgComBox::OnBnClickedOk()
{
    //获取用户选择的项
    int nIdx = m_cmbLocation.GetCurSel();

    //根据用户选择的项获取字符串
    CString strText;
    m_cmbLocation.GetLBText(nIdx, strText);

    //遍历链表，找到其值
    for (auto& Item : m_FileHeadMachineData)
    {
        if (Item.strExplain == strText)
        {
            *m_pwValue = Item.wValue;

            //将该值写入文件
            MyWriteFile(&Item.wValue);

            break;
        }
    }

    //关闭对话框
    PostMessageW(WM_CLOSE);
}


//取消按钮
void CMyDlgComBox::OnBnClickedCancle()
{
	PostMessageW(WM_CLOSE);
}


BOOL CMyDlgComBox::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	//如果是文件头中的Machine
	if (m_dwFlag == FILE_MACHINE)
	{
		//将其相关数据插入链表
		PushListToFileHeaderMachine();

        //将链表中的数据插入ComBox
        ShowDataToComBox();
	}

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}
